package com.iemes.controller.forming;

import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.iemes.annotation.SystemLog;
import com.iemes.controller.index.BaseController;
import com.iemes.entity.CostcontrolFormMap;
import com.iemes.entity.UserFormMap;
import com.iemes.mapper.forming.CostcontrolMapper;
import com.iemes.plugin.PageView;
import com.iemes.util.Common;
import com.iemes.util.DateUtils;
@Controller
@RequestMapping("costcontrol")
public class CostcontrolController extends BaseController{
	@Inject
	private CostcontrolMapper costcontrolMapper;
	
	@RequestMapping(value = "statistics", method = RequestMethod.GET, produces = "text/html; charset=utf-8")
	public String examinepru(Model model) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
		// 通过工具类获取当前登录的bean
		UserFormMap userFormMap = (UserFormMap) Common.findUserSession(request);
		//如果不是主管
		if (userFormMap.get("site").equals("0")) {
			//进入页面操作
			return Common.BACKGROUND_PATH + "/forming/costcontrol/statistics_regulatorAdd";		
		}
		model.addAttribute("res", findByRes());
		return Common.BACKGROUND_PATH + "/forming/costcontrol/statistics_list";	
	}
	
	@ResponseBody
	@RequestMapping("statistics_findByPage")
	public PageView findByPage(String pageNow, String pageSize) {	
		CostcontrolFormMap costcontrolFormMap =getFormMap(CostcontrolFormMap.class);
		List<Map<String,Object>> costcontrol = costcontrolMapper.getCostcontrol(costcontrolFormMap);
		try {
			LuogicPaging(pageNow, pageSize, costcontrol);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return pageView;
	}
	
	@RequestMapping("statistics_add")
	public String addUI(Model model)throws Exception {
		return Common.BACKGROUND_PATH + "/forming/costcontrol/statistics_add";
	}

	@ResponseBody
	@RequestMapping("statistics_addEntity")
	@Transactional(readOnly=false)//需要事务操作必须加入此注解
	@SystemLog(module = "成型数据统计", methods = "成本统计统计-新增成本统计统计")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String addEntity() throws Exception {
		CostcontrolFormMap costcontrolFormMap =getFormMap(CostcontrolFormMap.class);
		costcontrolFormMap.put("Date", DateUtils.getStringDateTime());
		costcontrolFormMap.put("Status", 1);
		costcontrolMapper.addEntity(costcontrolFormMap);
		return "success";
	}
	
	@RequestMapping("statistics_edit")
	public String editUI(Model model) throws Exception {
		String id = getPara("id");
		CostcontrolFormMap findbyFrist = costcontrolMapper.findbyFrist("id", id, CostcontrolFormMap.class);
		model.addAttribute("costcontrol", findbyFrist);	
		return Common.BACKGROUND_PATH + "/forming/costcontrol/statistics_edit";
	}

	@ResponseBody
	@RequestMapping(value = "statistics_editEntity")
	@SystemLog(module = "成型数据统计", methods = "成本统计统计统计-修改成本统计统计")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String editEntity() throws Exception {
		CostcontrolFormMap costcontrolFormMap =getFormMap(CostcontrolFormMap.class);
		costcontrolFormMap.put("Date", DateUtils.getStringDateTime());
		costcontrolMapper.editEntity(costcontrolFormMap);		
		return "success";
	}
	
	@ResponseBody
	@RequestMapping(value = "statistics_deleteEntity")
	@SystemLog(module = "成型数据统计", methods = "成本统计统计-删除成本统计统计")
	// 凡需要处理业务逻辑的.都需要记录操作日志
	public String deleteEntity() throws Exception {
		String[] ids = getParaValues("ids");
		String arr = ids[0];
		String[] str = arr.split(",");
		CostcontrolFormMap costcontrolFormMap = new CostcontrolFormMap();
		for (int i = 0; i < str.length; i++) {
			costcontrolFormMap.put("Status", 2);
			costcontrolFormMap.put("id", str[i]);
			costcontrolFormMap.put("Date", DateUtils.getStringDateTime());
			costcontrolMapper.editEntity(costcontrolFormMap);
		}
		return "success";
	}
}
